package java_thinking.unit_14._14_2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :   泛型语法案例，CountedInteger存储了一个类的引用，然后又产生了一个List，
 *                  填充这个List的对象是使用newInstance()
 * @author: cww
 * @DateTime: 2019-03-27 23:18
 */

class CountedInteger {
    private static long counter ;
    private final long id = counter++;
    @Override
    public String toString(){
        return Long.toString(id);
    }
}
/**
 * @author asd730520@live.com
 */
public class FilledList<T> {
    private Class<T> type;
    public FilledList(Class<T> type) {
        this.type = type;
    }
    public List<T> create(int nElements) {
        List<T> result = new ArrayList<>();
        try {
            for (int i =0;i<nElements;i++){
                result.add(type.newInstance());
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
        return result;
    }

    public static void main(String[] args) {
        FilledList<CountedInteger> list = new FilledList<CountedInteger>(CountedInteger.class);
        System.out.println(list.create(15));;

    }
}
